﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CRMSimple.LogService.Configuration.Abstractions;
using System.Threading;
using System.Reflection;

namespace CRMSimple.LogService.Configuration
{
    /// <summary>
    /// 配置类
    /// </summary>
    public abstract class ConfigurationRoot : IConfiguration
    {
        protected IDictionary<string, string> _properties = new Dictionary<string, string>();

        public string this[string key]
        {
            get
            {
                string value=string.Empty;
                if (_properties.TryGetValue(key, out value))
                    return value;
                return null;
            }
            set
            {
                _properties[key] = value;
            }
        }

        public IDictionary<string, string> Properties
        {
            get { return _properties; }
        }

        public virtual T ResolveModule<T>()where T:class ,new()
        {
            //创建新实体
            Type t=typeof(T);
            T info = Activator.CreateInstance(t, null) as T;//创建指定类型实例
            PropertyInfo[] fields =t.GetProperties();//获取指定对象的所有公共属性
            foreach (var field in fields)
	        {
                field.SetValue(info, this[field.Name],null);
	        }
            return info;
        }


        public abstract void Load();

        public abstract void Write();
    }
}
